package dji.ux.ext.widget.gimbal;

import androidx.annotation.NonNull;

import dji.common.error.DJIError;
import dji.common.gimbal.Axis;
import dji.common.product.Model;
import dji.common.util.CommonCallbacks;
import dji.sdk.sdkmanager.DJISDKManager;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.WidgetModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

/**
 * Widget Model for the {@link DualGimbalWidgetModel} used to define
 * the underlying logic and communication
 */
public class DualGimbalWidgetModel extends WidgetModel {


    public DualGimbalWidgetModel(@NonNull DJISDKModel djiSdkModel,
                                 @NonNull ObservableInMemoryKeyedStore keyedStore) {
        super(djiSdkModel, keyedStore);
    }

    /**
     * if the product has two gimbal devices and both connected, return true.
     *
     * @return boolean result
     */
    public boolean isSupportedDualGimbal() {
        if (DJISDKManager.getInstance().getProduct() == null) {
            return false;
        }

        Model model = DJISDKManager.getInstance().getProduct().getModel();
        if (model == null) {
            return false;
        } else {
            if (model.equals(Model.MATRICE_210) ||
                    model.equals(Model.MATRICE_210_RTK) ||
                    model.equals(Model.MATRICE_210_RTK_V2) ||
                    model.equals(Model.MATRICE_210_V2)) {

                //todo... 判断两个云台都正确连接相机
                return true;
            } else {
                return false;
            }
        }
    }

    public void getDualGimbalSwitchOn(CommonCallbacks.CompletionCallbackWith<Boolean> callback) {

        if (DJISDKManager.getInstance().getProduct() == null ||
                DJISDKManager.getInstance().getProduct().getGimbal() == null) {
            if (callback != null) {
                callback.onSuccess(false);
            }
            return;
        }

        DJISDKManager.getInstance().getProduct().getGimbal().getAttitudeSynchronizationEnabled(Axis.PITCH, callback);
    }

    /**
     * set dual gimbal widget state
     *
     * @param callback return the state of dual gimbal switch
     */
    public void setDualGimbalSwitch(CommonCallbacks.CompletionCallbackWith<Boolean> callback) {
        if (DJISDKManager.getInstance().getProduct() == null ||
                DJISDKManager.getInstance().getProduct().getGimbal() == null) {
            if (callback != null) {
                callback.onFailure(DJIError.COMMON_UNKNOWN);
            }
            return;
        }

        getDualGimbalSwitchOn(new CommonCallbacks.CompletionCallbackWith<Boolean>() {
            @Override
            public void onSuccess(Boolean aBoolean) {
                if (aBoolean != null) {
                    DJISDKManager.getInstance().getProduct().getGimbal().setAttitudeSynchronizationEnabled(!aBoolean, djiError -> {
                        if (djiError != null) {
                            if (callback != null) {
                                callback.onFailure(djiError);
                            }
                        } else {
                            if (callback != null) {
                                callback.onSuccess(!aBoolean);
                            }
                        }
                    });
                }
            }

            @Override
            public void onFailure(DJIError djiError) {
                if (callback != null) {
                    callback.onFailure(DJIError.COMMON_UNKNOWN);
                }
            }
        });

    }


    @Override
    protected void inSetup() {

    }

    @Override
    protected void inCleanup() {
        //Nothing to cleanup
    }

    @Override
    protected void updateStates() {
        //Nothing to update
    }
}
